package com.zzuli.controller;

import com.zzuli.entity.DeviceInfoEntity;
import com.zzuli.entity.Result;
import com.zzuli.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceInfoController {
    @Autowired
    private DeviceInfoService deviceInfoService;

    @GetMapping("/selectByPage")
    public Result selectByPage(Integer page, Integer limit, DeviceInfoEntity deviceInfoEntity) {
        Map<String, Object> data = deviceInfoService.selectAll(page, limit, deviceInfoEntity);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(data);
        return result;
    }

    @PostMapping("/addDevice")
    public Result addDevice(DeviceInfoEntity deviceInfoEntity){
        Result result = new Result();
        boolean flag = deviceInfoService.addDevice(deviceInfoEntity);
        if (flag) {
            result.setCode(200);
            result.setMsg("设备添加成功");
        }else {
            result.setCode(500);
            result.setMsg("设备添加失败");
        }
        return result;
    }

    @PostMapping("/updateDevice")
    public Result updateDevice(DeviceInfoEntity deviceInfoEntity){
        Result result = new Result();
        boolean flag = deviceInfoService.updateDevice(deviceInfoEntity);
        if (flag) {
            result.setCode(200);
            result.setMsg("设备修改成功");
        } else {
            result.setCode(500);
            result.setMsg("设备修改失败");
        }
        return result;
    }

    @PostMapping("/deleteDevice")
    public Result deleteUser(HttpServletRequest request) {
        Result result = new Result();
        String[] deviceIds = request.getParameterValues("allDeviceId[]");
        boolean flag = deviceInfoService.deleteDevice(deviceIds);
        if (flag){
            result.setCode(200);
            result.setMsg("设备删除成功");
        }else {
            result.setMsg("设备删除失败");
        }
        return result;
    }

}
